package com.sgb.kjwl.view.ui.activity.self.mine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jyb.qiyefuwu.router.PathHome;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.auction.activity.AuctionManageActivity;
import com.sgb.auction.activity.AuctionPublishActivity;
import com.sgb.auction.activity.BiddingQueryActivity;
import com.sgb.capital.view.ui.activity.CapitalActivity;
import com.sgb.goods.utils.Constants;
import com.sgb.jicai.view.activity.FactoryJicaiActivity;
import com.sgb.jicai.view.activity.JcChanceListActivity;
import com.sgb.jicai.view.activity.JcMyPriceActivity;
import com.sgb.jicai.view.activity.MyCenterMyJoinActivity;
import com.sgb.jicai.view.activity.MyCenterXuFangFaQiActivity;
import com.sgb.jicai.view.activity.MyJicaiSupervisionActivity;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySecondorderMoreBinding;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.sgb.kjwl.model.entity.self.SecondMore;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.MineAdapterItemClick;
import com.sgb.kjwl.view.ui.activity.EmptyNullActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.AddNeedListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.ContanctsActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverPublishMainActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.MySignUpListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.ProfessionalTeamPictureActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.PublishServiceListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.ServiceQuoteListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.TalentListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.TeamIntelligenceListActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.UserCenterTeamDetailActivity;
import com.sgb.kjwl.view.ui.activity.leaseAsk.MyAskLeaseActivity;
import com.sgb.kjwl.view.ui.activity.manage.WebSiteAddressActivity;
import com.sgb.kjwl.view.ui.activity.manage.WebSiteGroupActivity;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteMyXiXinActivity;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteOrganizationActivity;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteQiYeXiXinActivity;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickExpertListActivity;
import com.sgb.kjwl.view.ui.activity.self.ApprovalManagerActvity;
import com.sgb.kjwl.view.ui.activity.self.CarManagerActivity;
import com.sgb.kjwl.view.ui.activity.self.JoinCompanyActivity;
import com.sgb.kjwl.view.ui.activity.self.LogisticsMenuActivity;
import com.sgb.kjwl.view.ui.activity.self.PersonInfoActivity;
import com.sgb.kjwl.view.ui.activity.self.authentication.AuthActivity3;
import com.sgb.kjwl.view.ui.activity.self.authentication.CertificationActivity;
import com.sgb.kjwl.view.ui.activity.self.carMaster.BusinessOpportunitySubscriptionActivity;
import com.sgb.kjwl.view.ui.activity.self.carMaster.CarriageManageActivity;
import com.sgb.kjwl.view.ui.activity.self.carMaster.MyOfferActivity;
import com.sgb.kjwl.view.ui.activity.self.lease.LeaseMemberListActivity;
import com.sgb.kjwl.view.ui.activity.self.lease.SelfLeaseDemandListActivity;
import com.sgb.kjwl.view.ui.activity.self.lease.SelfLeaseOrderListActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.AddCarActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.BiddingOrderActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.CargoManageActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.PublishCargoActivity;
import com.sgb.kjwl.view.ui.activity.self.offer.TruckRegisterActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.LaborServiceAndEmploymentPublishListActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfEmploymentApplyActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfPackersManageActivity2;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamAchieveActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamAlbumActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamDeviceActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamEmploymentActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamGroupActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamIntroduceEditActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamMembersActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamOfferActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfWorkerPublishActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsListActivity3;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsPublishActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.PublicMyPriceListActivity;
import com.sgb.kjwl.view.ui.activity.self.publish.SelfLeaseListActivity;
import com.sgb.kjwl.view.ui.activity.self.team.TeamFunctionActivity;
import com.sgb.kjwl.view.ui.activity.transport.AddSelfCarActivity;
import com.sgb.kjwl.view.ui.activity.transport.MainCitiesActivity;
import com.sgb.kjwl.view.ui.activity.transport.OwnerAndDriverInquiryActivity;
import com.sgb.kjwl.view.ui.activity.transport.SelfCarManagerActivity;
import com.sgb.kjwl.view.ui.activity.transport.company.FleetManagementActivity;
import com.sgb.kjwl.view.ui.activity.transport.driver.order.TransportOrderDriverActivity;
import com.sgb.kjwl.view.ui.activity.transport.owner.FreightCarTransportationActivity;
import com.sgb.kjwl.view.ui.activity.transport.owner.TransportOrderOwnerActivity;
import com.sgb.kjwl.view.ui.adapter.pick.ChannelAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.ItemDragCallback;
import com.sgb.kjwl.view.ui.adapter.pick.SecondMoreAdapter;
import com.sgb.kjwl.view.widget.self.MenuClickUtils;
import com.sgb.link.router.PathDriver;
import com.sgb.link.router.PathGoods;
import com.sgb.link.router.PathLogin;
import com.sgb.link.router.PathLogistics;
import com.sgb.link.router.PathMerchant;
import com.sgb.link.router.PathTeam;
import com.sgb.link.router.PathTrip;
import com.sgb.member.ui.activity.MemberAuditActivity;
import com.sgb.member.ui.activity.MemberClientManageActivity;
import com.sgb.member.ui.activity.MemberDoorActivity;
import com.sgb.member.ui.activity.MemberInviteOrApplyActivity;
import com.sgb.member.ui.activity.MemberLeverSettingActivity;
import com.sgb.member.ui.activity.MemberManageActivity;
import com.sgb.member.ui.activity.RatingBusinessActivity;
import com.sgb.member.ui.activity.RemoveMemberActivity;
import com.sgb.zhaocai.view.activity.PickInvitedApplyListActivity;
import com.sgb.zhaocai.view.demand.activity.DemandIssueListActivity;
import com.sgb.zhaocai.view.demand.activity.OfferQueryListActivity;
import com.sgb.zhaocai.view.demand.activity.ProjectIssueListActivity;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.event.EventConstant;
import com.swgk.core.util.ACAdminDialogUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.UiUtil;
import com.swgk.core.util.UserOperating;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SecondMoreActivity extends AppActivity {
    private ChannelAdapter adapter;//首页菜单适配器
    private ActivitySecondorderMoreBinding mBinding;
    private List<PermissionMenuEntity.ListEntity> mlist=new ArrayList<>();//首页菜单数据
    private List<PermissionMenuEntity.ListEntity> mlist1=new ArrayList<>();//临时数据
    private boolean isbianji =false;//当前编辑状态
    private boolean isxian =true;//首页菜单折叠状态
    private SecondMoreAdapter secondMoreAdapter;//下面配置菜单适配器
    private List<PermissionMenuEntity> secondlist =new ArrayList<>();//下面配置菜单数据
    private SecondmoreModel model;
    private String companyNo="";//网站no
    private  int companyType;//网站type
    private boolean ismoren= true;//是否加载默认的菜单
    private String id="";//常用菜单id
    public static void start(Context context,ArrayList<PermissionMenuEntity.ListEntity> mListTemp) {
        Intent intent = new Intent();
        intent.setClass(context, SecondMoreActivity.class);
        intent.putExtra("list",(Serializable) mListTemp);
        context.startActivity(intent);
    }
    @Override
    protected void initView() {
        mlist1 = (List<PermissionMenuEntity.ListEntity>) getIntent().getSerializableExtra("list");
        if (mlist1.size()>16){
            mlist.addAll(mlist1.subList(0,15));
        }else {
            mlist.addAll(mlist1);
        }

        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_secondorder_more);
        model =new ViewModelProvider(SecondMoreActivity.this).get(SecondmoreModel.class);
        mBinding.include.titleView.setText("快捷菜单");
        mBinding.include.rightView.setText("管理");
        mBinding.include.rightView.setVisibility(View.VISIBLE);
        mBinding.include.rightView.setOnClickListener(this);
        mBinding.include.ivBack.setOnClickListener(this);
        mBinding.alltop.setOnClickListener(this);
        //获取常用应用列表
         companyType = getIntent().getIntExtra("mangertype",0);
        if (companyType==0) {
            companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        } else {
            companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        }
        //初始化默认菜单
        inittop();
        //初始化下面配置菜单
        initbottom();
        //获取权限菜单
        if (!TextUtils.isEmpty(companyNo)) {
            CompanyEntity entity = new CompanyEntity();
            entity.setCompNo(companyNo);
            entity.setType(companyType);
            if (SharedPreferenceUtil.getInstance().getSaveBooleanData(SharedPreferenceUtil.PERSON_AUTH, false)) {
                model.getPermissionMenu(entity,SecondMoreActivity.this);
            } else {
                model.getNoAuthPermissionMenu(entity,SecondMoreActivity.this);
            }

        }
    }

    @Override
    protected void init() {
        super.init();
    }

    //初始化默认菜单
  public void inittop(){
      GridLayoutManager manager = new GridLayoutManager(SecondMoreActivity.this, 4){
          @Override
          public boolean canScrollVertically() {
              return false;
          }
      };
      mBinding.rlvList.setLayoutManager(manager);
      adapter = new ChannelAdapter(SecondMoreActivity.this,  mlist);
      adapter.settype(companyType);
      ItemDragCallback callback = new ItemDragCallback(adapter);
      ItemTouchHelper helper = new ItemTouchHelper(callback);
      helper.attachToRecyclerView(mBinding.rlvList);
      mBinding.rlvList.setAdapter(adapter);
      adapter.setonRovedClicklistener(new ChannelAdapter.RovedClicklistensr() {
          @Override
          public void onRovedClicklistener(PermissionMenuEntity.ListEntity listEntity) {
                for (int a=0;a<secondlist.size();a++){
                    for (int b=0;b<secondlist.get(a).getList().size();b++){
                        for (int c=0;c<secondlist.get(a).getList().get(b).getList().size();c++){
                            if (listEntity.getTitle().equals(secondlist.get(a).getList().get(b).getList().get(c).getTitle())){
                                secondlist.get(a).getList().get(b).getList().get(c).setType(0);
                            }
                        }
                    }
                }
                secondMoreAdapter.notifyDataSetChanged();
          }

          @Override
          public void onItemClicklinener(PermissionMenuEntity.ListEntity listEntity) {
              MineAdapterItemClick.getInstance().adapterItemClick(listEntity,SecondMoreActivity.this);
          }
      });

  }
  //初始化下面配置菜单
  public void initbottom(){
        secondMoreAdapter= new SecondMoreAdapter(R.layout.item_secondmore,secondlist);
      LinearLayoutManager linearLayoutManager =new LinearLayoutManager(SecondMoreActivity.this){
          @Override
          public boolean canScrollVertically() {
              return false;
          }
      };
      mBinding.mRecyclerView.setLayoutManager(linearLayoutManager);
      mBinding.mRecyclerView.setAdapter(secondMoreAdapter);
      secondMoreAdapter.setClickAddlistener(new SecondMoreAdapter.onClickAddlistener() {
          @Override
          public void addlistener(int threepos, int twopos, int onepos) {
              if (adapter.getlist().size()==15){
                  ToastUtil.show(SecondMoreActivity.this,"您最多可以添加15个常用功能");
                  return;
              }
              PermissionMenuEntity.ListEntity permissionMenuEntity =secondlist.get(onepos).getList().get(twopos).getList().get(threepos);
              secondlist.get(onepos).getList().get(twopos).getList().get(threepos).setType(1);
              adapter.AddList(permissionMenuEntity);
              secondMoreAdapter.notifyDataSetChanged();
          }

          @Override
          public void addItemlistener(int threepos, int twopos, int onepos) {
              MineAdapterItemClick.getInstance().adapterItemClick(secondlist.get(onepos).getList().get(twopos).getList().get(threepos),SecondMoreActivity.this);
          }
      });
      MineAdapterItemClick.getInstance().setOnOnUrlMapDataCallBacklistener(new MineAdapterItemClick.UrlMapDataCallBack() {
          @Override
          public void OnUrlMapDataCallBacklistener(Map<String, String> map,String from) {
              model.getStringUrl(SecondMoreActivity.this,map);
          }
      });
//      secondMoreAdapter.getMutableLiveData().observe(this, new Observer<Map<String, String>>() {
//          @Override
//          public void onChanged(Map<String, String> map) {
//              model.getStringUrl(SecondMoreActivity.this,map);
//          }
//      });
  }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.right_view:
                isxian =false;
                mBinding.rlvList.setVisibility(View.VISIBLE);
                mBinding.jiantou.setImageResource(R.mipmap.lidou_up_bg);
                if (!isbianji){
                    mBinding.include.rightView.setText("保存");
                    isbianji =true;
                    if (ismoren){
                            mlist.clear();
                            adapter.getlist().clear();
//                        }
                    }
                }else {
                    Map<String,Object> map =new HashMap<>();
                    map.put("clientType",1);
                    map.put("id",id);
                    map.put("siteNo",companyNo);
                    map.put("siteType",companyType);
                    map.put("userNo",SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
                    List<Map<String,Object>> list= new ArrayList<>();
                    for (int i=0;i<adapter.getlist().size();i++){
                        Map<String,Object> map1 =new HashMap<>();
                        map1.put("menuId",adapter.getlist().get(i).getId());
                        if (adapter.getlist().get(i).getTitle().equals("邀请会员")||adapter.getlist().get(i).getTitle().equals("申请会员")
                        ||adapter.getlist().get(i).getTitle().equals("员工加入")){
                            map1.put("default",true);
                        }else {
                            map1.put("default",false);
                        }
                        list.add(map1);
                    }
                    map.put("iconList",list);
                    model.updateMenuOrder(SecondMoreActivity.this,map);
                    mBinding.include.rightView.setText("管理");
                    isbianji =false;
                }
                adapter.setisEdit(isbianji);
                secondMoreAdapter.setisbianji(isbianji);
                break;
            case R.id.alltop:
                if (!isxian){
                    isxian =true;
                    mBinding.rlvList.setVisibility(View.GONE);
                    mBinding.jiantou.setImageResource(R.mipmap.lidou_down_bg);
                }else {
                    isxian =false;
                    mBinding.rlvList.setVisibility(View.VISIBLE);
                    mBinding.jiantou.setImageResource(R.mipmap.lidou_up_bg);
                }
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
    public void showPermissionMenu(List<PermissionMenuEntity> list){
             list.remove(0);
             secondlist.clear();
             secondlist.addAll(list);
             secondMoreAdapter.notifyDataSetChanged();
        model.GetToplist(SecondMoreActivity.this,1,companyNo,companyType);
    }
    public void showchagnyongMenu(SecondMore.DataDTO dataDTO){
        if (dataDTO==null||dataDTO.getIconList()==null){
            return;
        }
        id =dataDTO.getId();
        if (dataDTO.getIconList().size()>0){
            ismoren=false;
            adapter.notifyDataSetChanged();
            for (int d=0;d<mlist.size();d++){
            for (int a=0;a<secondlist.size();a++){
                for (int b=0;b<secondlist.get(a).getList().size();b++){
                    for (int c=0;c<secondlist.get(a).getList().get(b).getList().size();c++){
                        if (mlist.get(d).getTitle().equals(secondlist.get(a).getList().get(b).getList().get(c).getTitle())){
                            secondlist.get(a).getList().get(b).getList().get(c).setType(1);
                        }
                    }
                }
            }
            }
            secondMoreAdapter.notifyDataSetChanged();
        }

    }


}
